package LeetCode_1_100;

import java.util.*;

class Solution49 {
    public List<List<String>> groupAnagrams(String[] strs) {
        Map<String, List<String>> map = new HashMap<>();
        for (String s : strs) {
            int[] count = new int[26];
            for (char c : s.toCharArray()) {
                count[c - 'a']++;
            }
            StringBuilder keyBuilder = new StringBuilder();
            for (int i = 0; i < 26; i++) {
                keyBuilder.append('#');
                keyBuilder.append(count[i]);
            }
            String key = keyBuilder.toString();
            map.putIfAbsent(key, new ArrayList<>());
            map.get(key).add(s);
        }
        return new ArrayList<>(map.values());
    }
}

public class LeetCode49_groupAnagrams {
    public static void main(String[] args) {
        Solution49 solution = new Solution49();
        String[] strs1 = new String[]{"eat","tea","tan","ate","nat","bat"};
        String[] strs2 = new String[]{""};
        String[] strs3 = new String[]{"a"};
        System.out.println(Arrays.toString(strs1));
        System.out.println(solution.groupAnagrams(strs1));
        System.out.println(Arrays.toString(strs2));
        System.out.println(solution.groupAnagrams(strs2));
        System.out.println(Arrays.toString(strs3));
        System.out.println(solution.groupAnagrams(strs3));
    }
}
